﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using SuperFox.SuperServiceReference;
using System.Collections.ObjectModel;

namespace SuperFox
{
    public partial class Login : UserControl
    {
        public Login()
        {
            InitializeComponent();
        }

        

        private SuperServiceClient client;

        private void doLogin()
        {
            client = ServiceConfig.Connect();

            //TestTb.Text = client.Endpoint.Address.Uri.ToString();

            //return;

            client.loginCompleted += new EventHandler<loginCompletedEventArgs>(client_loginCompleted);

            client.loginAsync(UserTb.Text.Trim().ToLower(), PasswordTb.Password.Trim().ToLower());
        }

        // do the login
        private void LoginBtn_Click(object sender, RoutedEventArgs e)
        {

            // DEBUG CODE

            LoginBtn.IsEnabled = false;
            //LoginBtn.Visibility = System.Windows.Visibility.Collapsed;

            //if (Config.IN_DEBUG)
            //{
                
            //    UserTb.Text = "Laura";
            //    PasswordTb.Password = "data#3";

            //}
            //UserTb.Text = "zeroseven";
            //PasswordTb.Password = "password";

            doLogin();

            //System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding();
            //binding.MaxBufferSize = 2147483647;
            //binding.MaxReceivedMessageSize = 2147483647;

            //client = new SuperServiceClient(binding, new System.ServiceModel.EndpointAddress("http://localhost:49552/SuperService.svc"));

            //client.OpenAsync();
            //client.OpenCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_OpenCompleted);

            

            //client.DoWorkCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_DoWorkCompleted);
            //client.getIntCompleted += new EventHandler<getIntCompletedEventArgs>(client_getIntCompleted);
            
        }

        void client_loginCompleted(object sender, loginCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                if (e.Result != null)
                {

                    // give access


                    if ((App.Current.RootVisual as MainPage).CurrentUser != null)
                    {
                        return;
                    }


                    (App.Current.RootVisual as MainPage).CurrentUser = e.Result;



                    Style headerStyle = new Style(typeof(TextBlock));
                    //headerStyle.Setters.Add(new Setter(TextBlock.ForegroundProperty, Config.StringToColour((App.Current.RootVisual as MainPage).CurrentUser.Programs.Style.BackgroundColour)));
                    headerStyle.Setters.Add(new Setter(TextBlock.FontSizeProperty, 20));
                    headerStyle.Setters.Add(new Setter(TextBlock.ForegroundProperty, new SolidColorBrush(Config.StringToColour((App.Current.RootVisual as MainPage).CurrentUser.Programs.Style.HeaderTextColour))));
                    headerStyle.Setters.Add(new Setter(TextBlock.MarginProperty, new Thickness(20, 0, 0, 0)));

                    if(!App.Current.Resources.Contains("ClientHeaderStyle"))
                        App.Current.Resources.Add("ClientHeaderStyle", headerStyle);


                    clientWelcomePage wp = new clientWelcomePage();
                    
                    wp.ClientNameTb.Text = e.Result.Programs.merc_name;
                    wp.Products = e.Result.Programs.Products;

                    clientSideBar csb = new clientSideBar();

                    csb.ClientName.Text = "Welcome " + e.Result.log_username;

                    (App.Current.RootVisual as MainPage).LoadContent(wp);
                    (App.Current.RootVisual as MainPage).LoadSidbar(csb);
                    (App.Current.RootVisual as MainPage).LoadHeader(new ClientHeader());
                    //(App.Current.RootVisual as MainPage).MainBackground.Children.Clear();
                    (App.Current.RootVisual as MainPage).MainContent.Background = new SolidColorBrush(Config.StringToColour((App.Current.RootVisual as MainPage).CurrentUser.Programs.Style.FillColour));
                    (App.Current.RootVisual as MainPage).LowerBackground.Background = new SolidColorBrush(Config.StringToColour((App.Current.RootVisual as MainPage).CurrentUser.Programs.Style.FillColour));
                    //(App.Current.RootVisual as MainPage).FooterRepeater.Children.Clear();
                    (App.Current.RootVisual as MainPage).FooterControlGrid.Background = new SolidColorBrush(Config.StringToColour((App.Current.RootVisual as MainPage).CurrentUser.Programs.Style.FooterColour));
                    //(App.Current.RootVisual as MainPage).MainTilePanel.Visibility = System.Windows.Visibility.Collapsed;
                    //(App.Current.RootVisual as MainPage).FooterTilePanel.Visibility = System.Windows.Visibility.Collapsed;

                    /*
                     * <Style x:Name="SubHeaderRed" TargetType="TextBlock">
                            <Setter Property="Foreground" Value="#e31b23"></Setter>
                            <Setter Property="FontSize" Value="20"></Setter>
                            <Setter Property="Margin" Value="20,0,0,0"></Setter>
                            <Setter Property="FontFamily" Value="Trebuchet MS"></Setter>
                        </Style>
                     * */


                   

                    //UserTb.Text = "Ok";
                }
                else
                {
                    FailedLoginTb.Visibility = System.Windows.Visibility.Visible;
                    LoginBtn.IsEnabled = true;
                    //LoginBtn.Visibility = System.Windows.Visibility.Visible;
                }
            }
            else
            {
                TestTb.Text = e.Error.StackTrace;
                LoginBtn.IsEnabled = true;
            }
        }

        private void PasswordTb_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                doLogin();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            // see if there is a user already logged in

            if ((App.Current.RootVisual as MainPage).CurrentUser != null)
            {
                clientWelcomePage wp = new clientWelcomePage();

                wp.ClientNameTb.Text = (App.Current.RootVisual as MainPage).CurrentUser.Programs.merc_name;
                wp.Products = (App.Current.RootVisual as MainPage).CurrentUser.Programs.Products;

                clientSideBar csb = new clientSideBar();

                csb.ClientName.Text = "Welcome " + (App.Current.RootVisual as MainPage).CurrentUser.log_username;

                (App.Current.RootVisual as MainPage).LoadContent(wp);
                (App.Current.RootVisual as MainPage).LoadSidbar(csb);
                (App.Current.RootVisual as MainPage).LoadHeader(new ClientHeader());

                //(App.Current.RootVisual as MainPage).MainBackground.Children.Clear();

                (App.Current.RootVisual as MainPage).MainContent.Background = new SolidColorBrush(Config.StringToColour((App.Current.RootVisual as MainPage).CurrentUser.Programs.Style.FillColour));
                (App.Current.RootVisual as MainPage).LowerBackground.Background = new SolidColorBrush(Config.StringToColour((App.Current.RootVisual as MainPage).CurrentUser.Programs.Style.FillColour));
                //App.Current.RootVisual as MainPage).FooterRepeater.Children.Clear();
                (App.Current.RootVisual as MainPage).FooterControlGrid.Background = new SolidColorBrush(Config.StringToColour((App.Current.RootVisual as MainPage).CurrentUser.Programs.Style.FooterColour));
                //(App.Current.RootVisual as MainPage).MainTilePanel.Visibility = System.Windows.Visibility.Collapsed;
                //(App.Current.RootVisual as MainPage).FooterTilePanel.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        //void client_getIntCompleted(object sender, getIntCompletedEventArgs e)
        //{
        //    if (e.Error == null)
        //    {
        //        UserTb.Text = e.Result.ToString();
        //    }
        //    else
        //    {
        //        TestTb.Text = e.Error.StackTrace;
        //    }
        //}

        //void client_OpenCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        //{
        //    if (e.Error == null)
        //    {
        //        UserTb.Text = "State:" + client.State.ToString();
        //        client.DoWorkAsync();

        //        //UserTb.Text = "do work";
        //    }
        //    else
        //    {
        //        TestTb.Text = e.Error.StackTrace;
        //    }
        //}

        //void client_DoWorkCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        //{
        //    if (e.Error == null)
        //    {
        //        UserTb.Text = "OK";
        //    }
        //    else
        //    {
        //        TestTb.Text = e.Error.StackTrace;
        //    }
        //}




    }
}
